<?php
	
	include_once("cmsConfig.php");
	include_once("lib/CmsWriter.php");
	$authInfo = extractObjectWithKeyFromArray('authInfo', $_SESSION);
	
	//$cmsDocument->openCurrentDocument("5119");
	//echo "document = <pre>";print_r($cmsDocument);echo "</pre>";
	//echo "content = ".$cmsDocument->content;
	
	$gid = extractNumberWithKeyFromArray('gid', $_REQUEST);// echo "gid = $gid<br>";
	$organisationalname = extractNumberWithKeyFromArray('organisationalname', $_REQUEST);// echo "organisationalname = $organisationalname<br>";
	$processFormData = extractStringWithKeyFromArray("processFormData", $_REQUEST, "NO");
	$processFormData = $processFormData == "YES";
	
	if($processFormData == "YES") {
		$codesSaved = array();
		foreach ($_FILES as $filekey=>$fileValue) {
			if($_FILES[$filekey]['size'] != '0') {
				$formIdSave = "130";
				$documentCodeSave = null;
				$_FILES['content'] = $_FILES[$filekey];
				//$_POST[content] = 
				$cmsWriter = new CmsWriter( $formIdSave, $documentCodeSave, $_POST, $authInfo, $db);
				$message = $cmsWriter->processFormData();
				$codesSaved[] = $cmsWriter->documentCode;
				/*echo "Post =<br><pre>";print_r($_POST);echo "</pre><br>";
				echo "Files =<br><pre>";print_r($_FILES);echo "</pre>";
				echo "<br>Message = <br><pre>";print_r($message);echo "</pre>";
				echo "document code = ".$cmsWriter->formData['documentcode']." ";
				echo "<br>authinfo = <pre>";print_r($authInfo);echo "</pre>";*/
			}
		}
	
		//get the passed in return url, and set it in the session.
		$returnURL = extractStringWithKeyFromArray('returnURL', $_REQUEST);
		if (!is_null($returnURL)) {
			$_SESSION['returnURL'] = $returnURL;
		}
		
		if (is_null($returnURL)) {
			//once we get this far, we need to strip/ clear the form/document stuff from the session..
			$returnURL = CMS_HOME . "?fileNo=1&folder=".$organisationalname."&formid=" . $formIdSave;
		}
		
		$documentType = getDocumentTypeForDocumentTypeCode($cmsWriter->formData['documenttypecode'], $db);
		$message->addValidationMessageForKey("CmsWrite","db_OBJECT_SAVED",CMS_STATUS_NOTICE);
		$m = "";
		foreach ($codesSaved as $codes) {
			$m .= "The '" . $documentType->documenttypedescription . "' with code ". $codes . " was saved successfully.<br>";
		}
			dialoguePage(array(	DLG_MESSAGE=> 	$m,
					DLG_LEFT_TEXT=>	DLG_EMPTY,
					DLG_LEFT_LINK=>	CMS_HOME,
					DLG_RIGHT_TEXT=>	DLG_OK,
					DLG_RIGHT_LINK=>	urldecode($returnURL) ));	
	
	} else {
	
		
		
		$CmsTableWriter = new CmsTableWriter(CMS_FORM_DESC_FORMAT, $db);
		$formDescription = $formDescriptorTableWriter->readItem("130");
		$element = new CmsFormElement($formDescription, $db);
		
?>
<div class="cms_browse_heading_label">
<h1>Upload Multiple Images</h1>
</div>
<div class="cms_browse_addrecord">
</div>
<div class="cms_browse_divider" id="cms_browse_topdivider">
</div>
<script src="/cms/html/js/multiple-file-element/multifile_compressed.js"></script>
<form id="Image Library" name="Image Library" enctype="multipart/form-data" method="post" action="/cms/html/index.php?fileNo=104" class="cms_form">
<input type="hidden" value="YES" name="processFormData">
<input type="hidden" name="gid" value="<?=$gid?>">


<div class="cms_masteredit">
<table width="100%" cellspacing="0" cellpadding="0" border="0" class="cms_masteraddedit_table">
<tbody>
<tr>
<td width="160" height="1">
<img width="160" height="1" src="/cms/images/spacer.gif">
</td>
<td height="1">
<img width="1" height="1" src="/cms/images/spacer.gif">
</td>
</tr>
<tr class="cms_masteraddedit_table" id="cms_masteredit_130_10026">
<td class="cms_form_label" id="cms_form_label_documentcode">

Document Code:

</td>
<td align="left" class="cms_form_element">
<span name="documentcode" class="cms_form_textlabel" id="cms_form_documentcode">
</span>
</td>
</tr>
<tr class="cms_masteraddedit_table" id="cms_masteredit_130_11119">
<td class="cms_form_label" id="cms_form_label_documenttypecode">

Document Type:

</td>
<td align="left" class="cms_form_element">
<select class="cms_form_dropdown" id="cms_form_documenttypecode" name="documenttypecode">
<option value="docImageLib">

Image Library

</option>
</select>
<script>

for (i=0; i < document.forms['Image Library'].elements['documenttypecode'].options.length; i++){
if(document.forms['Image Library'].elements['documenttypecode'].options[i].value=='docImageLib'){
document.forms['Image Library'].elements['documenttypecode'].selectedIndex=i;
}
}

</script>
</td>
</tr>
<tr class="cms_masteraddedit_table" id="cms_masteredit_130_10001">
<td class="cms_form_label" id="cms_form_label_datelogged">

Last Updated:

</td>
<td align="left" class="cms_form_element">
<?
	$elementDescriptor = $element->getElementDescriptor("10001");
	print("<span id=\"cms_form_datelogged\" class=\"cms_form_textlabel\" name=\"datelogged\" >" . $element->getDefault($elementDescriptor) . "</span>");
?>
</td>
</tr>
<tr class="cms_masteraddedit_table" id="cms_masteredit_130_11120">
<td class="cms_form_label" id="cms_form_label_title">

Caption

</td>
<td align="left" class="cms_form_element">
<textarea class="cms_form_textarea" id="cms_form_title" name="title">
</textarea>
</td>
</tr>
<? 
/*<tr class="cms_masteraddedit_table" id="cms_masteredit_130_11121">
<td class="cms_form_label" id="cms_form_label_content">

Image:

</td>
<td align="left" class="cms_form_element">
<input type="hidden" value=",," name="content">
<input type="file" class="cms_form_file" id="cms_form_content" name="content">
</td>
</tr>*/
?>
<tr class="cms_masteraddedit_table" id="cms_masteredit_130_11121">
<td class="cms_form_label" id="cms_form_label_content">
Choose up to 5 Images:
</td>
<td align="left" class="cms_form_element">
<input type="hidden" value=",," name="content">
	<input id="cms_form_content" type="file" name="file_1" >
</td>
</tr>
<tr class="cms_masteraddedit_table">
<td class="cms_form_label">
Files:
</td>
<td align="left" class="cms_form_element">
<!-- This is where the output will appear -->
<div id="files_list"></div>
<script>
	<!-- Create an instance of the multiSelector class, pass it the output target and the max number of files -->
	var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 5 );
	<!-- Pass in the file element -->
	multi_selector.addElement( document.getElementById( 'cms_form_content' ) );
</script>
</td>
</tr>
<tr class="cms_masteraddedit_table" id="cms_masteredit_130_10029">
<td class="cms_form_label" id="cms_form_label_documentdate">

Date Added:

</td>
<td align="left" class="cms_form_element">
<?
	$elementDescriptor = $element->getElementDescriptor("10029");
	echo "<div class=\"cms_form_date\">";
		printDateInput(
			$elementDescriptor->name, 
			processAttribs(array("value"=>$element->getDefault($elementDescriptor), 'id' => 'cms_form_' . $elementDescriptor->name , 'class'=>'cms_form_date'),array())
		);
	echo "</div>";
?>
</td>
</tr>
<tr class="cms_masteraddedit_table" id="cms_masteredit_130_11215">
<td class="cms_form_label" id="cms_form_label_organisationalname">

Image Folder:

</td>
<td align="left" class="cms_form_element">

<?
	$elementDescriptor = $element->getElementDescriptor("11215");
	optionList(
			processAttribs(
						array("name"=>$elementDescriptor->name) ,  array( 'id' => 'cms_form_' . $elementDescriptor->name , 'class'=>'cms_form_dropdown' ) )
								,false
								,null
								,0,1,magicClean($elementDescriptor->sourcequery, 0),$db);
						//add the javascript here..
						
						
							
							print <<< EOD
<script>
        for (i=0; i < document.forms['Image Library'].elements['$elementDescriptor->name'].options.length; i++){
                if(document.forms['Image Library'].elements['$elementDescriptor->name'].options[i].value=='$organisationalname'){
                        document.forms['Image Library'].elements['$elementDescriptor->name'].selectedIndex=i;
                }
        }
</script>							
EOD;
						
?>
</td>
</tr>
<tr class="cms_masteraddedit_table" id="cms_masteredit_130_10025">
<td class="cms_form_label" id="cms_form_label_active">

Active?:

</td>
<td align="left" class="cms_form_element">
<input type="checkbox" class="cms_form_checkbox" id="cms_form_active" value="1" name="active">
<script>

document.forms['Image Library'].elements['active'].checked=true;

</script>
</td>
</tr>
<tr>
<td valign="middle" align="right" colspan="2">
<script type="text/javascript" language="javascript">

function selectAllElementsInSelect( select ) { for (i=0; i < select.options.length; i++){ select.options[i].selected = true; } return; } function doOnSubmitForElementsOnForm( formName ){ f = document.getElementById( formName ); if (f != null){ for (j = 0; j < f.length; j++ ){ e = f[j]; if (e.onsubmit){ e.onsubmit(); } } } return; }

</script>
<a href="javascript:doOnSubmitForElementsOnForm('Image Library');document.forms['Image Library'].submit();" class="cms_form_submit" id="cms_form_submit_130">
<img src="/cms/images/button_go.gif">
</a>
</td>
</tr>
</tbody>
</table>
</div>
</form>
<?
	}
?>